package com.jerry.hm.datastructure.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 单向链表
 *
 * @author Jerry
 * @since 2024/9/29 20:13
 */
public class SingleLinkedList implements Iterable<Integer> {

    /**
     * 头指针
     */
    Node head;

    /**
     * 节点
     */
    private static class Node {
        /**
         * 值
         */
        int value;

        /**
         * 下一个节点指针
         */
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头部添加元素
     *
     * @param value
     */
    public void addFirst(int value) {
        head = new Node(value, head);
    }

    /**
     * 寻找尾部节点
     *
     * @return
     */
    private Node findLast() {
        // 空链表
        if (head == null) {
            return null;
        }

        // 非空链表
        Node cur;
        for (cur = head; cur.next != null; cur = cur.next) {

        }
        return cur;
    }

    /**
     * 尾部添加元素
     *
     * @param value
     */
    public void addLast(int value) {
        Node tail = findLast();
        if (tail == null) {
            addFirst(value);
            return;
        }
        tail.next = new Node(value, null);
    }

    /**
     * 获取指定索引的元素
     *
     * @param index
     * @return
     */
    public int get(int index) {
        if (index < 0) {
            throw illegalException(index);
        }
        Node node = find(index);
        if (node == null) {
            throw illegalException(index);
        }
        return node.value;
    }

    /**
     * 获取指定索引的节点
     *
     * @param index
     * @return 找不到返回null，否则返回该节点
     */
    private Node find(int index) {
        int i = 0;
        Node cur;
        for (cur = head; cur != null; cur = cur.next, i++) {
            if (i == index) {
                return cur;
            }
        }

        return null;
    }

    /**
     * 向指定索引位置插入一个元素
     *
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        if (index < 0) {
            throw illegalException(index);
        }
        if (index == 0) {
            // 插入头部的情况
            addFirst(value);
            return;
        }
        Node pre = find(index - 1);
        if (pre == null) {
            throw illegalException(index);
        }
        pre.next = new Node(value, pre.next);
    }

    /**
     * 移除头部元素
     */
    public void removeFirst() {
        if (head == null) {
            // 链表为空
            throw illegalException(0);
        }
        head = head.next;
    }

    /**
     * 移除指定索引的元素
     *
     * @param index
     */
    public void remove(int index) {
        if (index < 0) {
            throw illegalException(index);
        }
        if (index == 0) {
            // 移除头部元素
            removeFirst();
            return;
        }
        Node pre = find(index - 1);
        if (pre == null) {
            throw illegalException(0);
        }
        Node target = find(index);
        if (target == null) {
            throw illegalException(0);
        }
        pre.next = target.next;
    }

    /**
     * 遍历1
     */
    public void loop1(Consumer<Integer> consumer) {
        Node cur = head;

        while (cur != null) {
            consumer.accept(cur.value);
            cur = cur.next;
        }
    }

    /**
     * 遍历2
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node cur = head; cur != null; cur = cur.next) {
            consumer.accept(cur.value);
        }
    }

    /**
     * 递归遍历
     *
     * @param before
     * @param after
     */
    public void loop3(Consumer<Integer> before, Consumer<Integer> after) {
        recursion(head, before, after);
    }

    /**
     * 递归
     *
     * @param cur
     * @param before
     * @param after
     */
    private void recursion(Node cur, Consumer<Integer> before, Consumer<Integer> after) {
        if (cur == null) {
            return;
        }
        before.accept(cur.value);
        recursion(cur.next, before, after);
        after.accept(cur.value);
    }

    @Override
    public Iterator<Integer> iterator() {

        return new Iterator<>() {

            Node cur = head;

            @Override
            public boolean hasNext() {
                return cur != null;
            }

            @Override
            public Integer next() {
                int value = cur.value;
                cur = cur.next;
                return value;
            }
        };
    }

    /**
     * 抛异常
     *
     * @param index
     * @return
     */
    private static RuntimeException illegalException(int index) {
        throw new IllegalArgumentException(String.format("index [%d] 不合法%n", index));
    }
}
